/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-09-25
 */
package org.jeecg.modules.flow.callback.inbound.task;

import cn.hutool.core.util.StrUtil;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.vo.WmsInReceiptTaskPage;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.utils.controller.DocumentStatusMoveController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库任务提交回调前bean
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-09-25
 */
@Component
public class WmsInReceiptTaskSubmitBeforeBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Autowired
    private DocumentStatusMoveController documentStatusMoveController;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        //保存
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        baseFlowDto.translateClass(WmsInReceiptTaskPage.class);
        WmsInReceiptTaskPage data = (WmsInReceiptTaskPage) baseFlowDto.getData();
        WmsInReceiptTask wmsInReceiptTask = new WmsInReceiptTask();
        BeanUtils.copyProperties(data, wmsInReceiptTask);
        String id = null;
        if (StrUtil.isBlank(data.getId())) {
            id = iWmsInReceiptTaskService.saveMain(wmsInReceiptTask, data.getWmsInReceiptTaskMaterialList());
            baseFlowDto.setInfoId(id);
        } else {
            id = data.getId();
        }
        //提交
        baseFlowDto.setResult(documentStatusMoveController.commitDocument(id));
        //任务提交之后为待执行
        WmsInReceiptTask po = iWmsInReceiptTaskService.getById(id);
        po.setDocumentStatus(DocumentStatusEnum.COMMIT.getCode());
        iWmsInReceiptTaskService.updateById(po);
        baseFlowDto.setInfoId(id);
    }
}
